package com.liuya.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MapStorgeColumnAdapter implements ColumnAdapter , java.io.Serializable {
	private String key;
	private String className;

	private ResultSetReader rsReader;

	public MapStorgeColumnAdapter(String mapKey) {
		this.key = mapKey;
		rsReader = new ResultSetReader.ObjectReader();
	}

	public MapStorgeColumnAdapter(String mapKey, String className) {
		this.key = mapKey;
		this.className = className;

		rsReader = ResultSetReaderUtil.getReader(className);
	}

	public void readResultSet(ResultSet rs, String columnName, Object o) throws SQLException {
		Object readValue = rsReader.reader(rs, columnName);
		MapStorgeObject mso = (MapStorgeObject) o;
		mso.put(key, readValue);

	}

	public void readResultSet(ResultSet rs, int columnIndex, Object o) throws SQLException {
		Object readValue = rsReader.reader(rs, columnIndex);

		MapStorgeObject mso = (MapStorgeObject) o;
		mso.put(key, readValue);
	}

	public void setPreparedStatement(PreparedStatement pstmt, int paramIndex, Object o) throws SQLException {
		MapStorgeObject mso = (MapStorgeObject) o;
		Object setValue = mso.get(key);

		if (setValue instanceof java.util.Date) {
			java.util.Date date = (java.util.Date) setValue;
			setValue = new java.sql.Date(date.getTime());
		}
		pstmt.setObject(paramIndex, setValue);
	}

	public Object extractFieldValue(Object o) {
		MapStorgeObject mso = (MapStorgeObject) o;
		return mso.get(key);
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}
}
